package com.supply.dl.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.supply.dl.R;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.adapter.NormalPositionAdapter;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.CreateMaterierSheetPostInfo;
import com.supply.dl.bean.FormulaInfo;
import com.supply.dl.bean.MaterialCollarInfo;
import com.supply.dl.bean.SelectDictionaryBean;
import com.supply.dl.utils.DialogUtils;
import com.supply.dl.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建领料单
 * <p>
 * Author:  tian <br/>
 * Time:  2018/4/25 23:54
 */
public class CreateMaterialSheetActivity extends BaseActivity {

    @BindView(R.id.material_rc)
    RecyclerView material_rc;

    private NormalPositionAdapter<MaterialCollarInfo> adapter;

    private List<MaterialCollarInfo> datas;

    /**
     * OMS单号
     */
    private String omsOrderNo;


    /**
     * 收货仓库
     */
    private SelectDictionaryBean inHouse;

    @BindView(R.id.store_tv)
    TextView store_tv;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_create_material_sheet;
    }

    @Override
    protected void initView() {
        showLeftBack();
        setTitleShow("新建领料单");

        omsOrderNo = getIntent().getStringExtra("omsOrderNo");

        datas = new ArrayList<>();
        adapter = new NormalPositionAdapter<MaterialCollarInfo>(this, datas) {
            @Override
            protected void convert(ViewHolder holder, final MaterialCollarInfo data, int positon) {
                holder.setText(R.id.material_name_tv, "物料" + (positon + 1));

                holder.setText(R.id.wl_code_tv, "物料编码:" + data.getSkuCode());
                holder.setText(R.id.wl_name_tv, "物料名称:" + data.getSkuName());

                StringBuffer pfBuffer = new StringBuffer();
                pfBuffer.append(data.getpAmount() + "件");
                //有重量的时候
                if (Integer.parseInt(data.getpWeight()) > 0) {
                    pfBuffer.append("," + data.getpWeight() + "吨");
                }
                //
                else if (Integer.parseInt(data.getpVolume()) > 0) {
                    pfBuffer.append("," + data.getpVolume() + "方");
                }

                holder.setText(R.id.peifang_nub_tv, "物料总量:" + pfBuffer.toString());
                holder.setText(R.id.djzl_tv, data.getPackageWeight());
                holder.setText(R.id.pichi_code_tv, "批次号:" + data.getBatchNo());

                ((EditText) holder.getView(R.id.count_jian_et)).addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                        if (StringUtils.isCanUse(s.toString())) {
                            data.setAmount(s.toString());
                        }
                    }
                });

                ((EditText) holder.getView(R.id.count_dun_et)).addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                        if (StringUtils.isCanUse(s.toString())) {
                            data.setWeight(s.toString());
                        }
                    }
                });

                ((EditText) holder.getView(R.id.count_fang_tv)).addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                        if (StringUtils.isCanUse(s.toString())) {
                            data.setVolume(s.toString());
                        }
                    }
                });

                ((EditText) holder.getView(R.id.mark_et)).addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                        if (StringUtils.isCanUse(s.toString())) {
                            data.setRemark(s.toString());
                        }
                    }
                });

                holder.getView(R.id.delete_tv).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.showDialog(CreateMaterialSheetActivity.this, "提示", "确定删除吗?", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                datas.remove(data);
                                adapter.notifyDataSetChanged();
                            }
                        }).show();
                    }
                });
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_material_sheet;
            }
        };

        material_rc.setLayoutManager(new LinearLayoutManager(this));
        material_rc.setAdapter(adapter);
    }


    @OnClick({R.id.store_tv, R.id.add_material_tv, R.id.save_tv})
    public void OnClick(View v) {
        switch (v.getId()) {

            case R.id.store_tv:

                //跳转去入库仓库
                Intent intent1 = new Intent(this, SelectBaseDataActivity.class);
                intent1.putExtra("action", AppConfig.BaseDataAction.WARE_HOUSE_TYPE_0);
                startActivityForResult(intent1, AppConfig.BaseDataAction.REQCODE_MOVE_IN_HOUSE_SELECT);

                break;

            case R.id.add_material_tv:
                Intent intent = new Intent(this, FormulaSelectActivity.class);
                intent.putExtra("omsOrderNo", omsOrderNo);
                startActivityForResult(intent, AppConfig.BaseDataAction.REQCODE_SELECT_FORMULA);
                break;

            case R.id.save_tv:
                createMaterierBill();
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null)
            return;

        //选配方
        if (requestCode == AppConfig.BaseDataAction.REQCODE_SELECT_FORMULA) {

            FormulaInfo formulaInfo = (FormulaInfo) data.getSerializableExtra("selectBean");
            MaterialCollarInfo mInfo = new MaterialCollarInfo();
            mInfo.setSkuId(formulaInfo.getSkuId());
            mInfo.setSkuCode(formulaInfo.getSkuCode());
            mInfo.setSkuName(formulaInfo.getSkuName());
            mInfo.setpAmount(formulaInfo.getQuantity());
            mInfo.setpWeight(formulaInfo.getWeight());
            mInfo.setpVolume(formulaInfo.getVolume());
            mInfo.setBatchNo(formulaInfo.getBatchNo());
            mInfo.setSupplierName(formulaInfo.getSupplierName());


            mInfo.setPackageWeight(formulaInfo.getPackageId());
            mInfo.setPackageId(formulaInfo.getPackageName());

            datas.add(mInfo);
            adapter.notifyDataSetChanged();
        }
        // 选仓库
        else if (requestCode == AppConfig.BaseDataAction.REQCODE_MOVE_IN_HOUSE_SELECT) {
            inHouse = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
            store_tv.setText(inHouse.getText());
        }
    }

    /**
     * 新建领料单
     */
    private void createMaterierBill() {

        if (inHouse == null) {
            Toast.makeText(this, "请选择仓库", Toast.LENGTH_SHORT).show();
            return;
        }

        if (datas.size() == 0) {
            Toast.makeText(this, "请添加物料", Toast.LENGTH_SHORT).show();
            return;
        }

        CreateMaterierSheetPostInfo info = new CreateMaterierSheetPostInfo();

        info.setTfOrderId(getIntent().getStringExtra("tfOrderId"));
        info.setTfOrderNo(getIntent().getStringExtra("tfOrderNo"));
        info.setInWhId(inHouse.getValue());
        info.setNoticeOrderDetails(datas);

        ApiService api = HttpMethod.getInstance().create(ApiService.class);
        api.createMaterierBill(info)
                .compose(RxSchedulers.<BaseInfo<String>>compose(this))
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public void onSuccess(BaseInfo<String> baseInfo) {

                        if (baseInfo != null && baseInfo.getData() != null) {
                            Bundle bundle = new Bundle();
                            bundle.putString("orderNo", baseInfo.getData());

                            skipActivity(CreateMaterialSuccessActivity.class, bundle);
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }
}










